package org.devocean.groupbuying.web.backend;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;


import org.devocean.groupbuying.domain.Menu;
import org.devocean.groupbuying.domain.MenuLocalization;
import org.devocean.groupbuying.service.MenuLocalizationService;
import org.devocean.groupbuying.utils.RenderMapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = { "/gb-cpanel/menuslocalizations" })
public class MenusLocalizationController {
	
	@Autowired
	private MenuLocalizationService menuLocalizationService;
	
	/**
	 * edit a menu
	 */
	
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public Map<String, ? extends Object> edit(@PathVariable("id") Integer id) {
		try {
			MenuLocalization menuLocalization = menuLocalizationService.findMenuById(id);
			Set<Menu> menus = menuLocalization.getMenus();
			Iterator<Menu> iterator = menus.iterator();
			Menu menu = iterator.next();
			Map<String, Object> data = new ConcurrentHashMap<String, Object>();
			data.put("menuLocalizationID", menuLocalization.getMenuLocalizationID());
			data.put("menuID", menu.getMenuID());
			data.put("title", menu.getTitle());
			data.put("lang", menu.getLang());
			data.put("permalink", menu.getPermalink() == null ? "" :menu.getPermalink() );
			data.put("layout", menu.getLayout() == null ? "" : menu.getLayout());
			data.put("disabled",
					menu.getDisabled() == null ? 0 : menu.getDisabled());
			data.put("view", menu.getView() == null ? "" : menu.getView());
			data.put("parentMenuID", menu.getMenu() == null ? 0 : menu.getMenu()
					.getMenuID());
			return RenderMapUtils.getMap(data);

		} catch (Exception e) {
			e.printStackTrace();
			return RenderMapUtils
					.getModelMapError("Error retrieving Menus from database.");
		}
	}
	
	/**
	 * add or update a menu
	 */	
	@RequestMapping(value = "/", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, ? extends Object> addOrUpdate(@RequestBody MenuLocalization menuLocalization) {
		menuLocalization = menuLocalizationService.saveOrUpdateMenuLocalization(menuLocalization);
		Map<String, Object> data = new ConcurrentHashMap<String, Object>();
		data.put("menuLocalizationID", menuLocalization.getMenuLocalizationID());
		Menu menu = menuLocalization.getMenus().iterator().next();
		data.put("title", menu.getTitle());
		data.put("menuID", menu.getMenuID());
		data.put("lang", menu.getLang());
		return RenderMapUtils.getMap(data);

	}
	
	/**
	 * edit other language for a menu
	 */
	
	@RequestMapping(value = "/localize", method = RequestMethod.GET)
	public Map<String, ? extends Object> localize(Integer LocalizationKey, String LocalizationRequest, Integer activeElementId) {
		try {

			MenuLocalization menuLocalization = menuLocalizationService.getMenuLocalizationById(LocalizationKey);
			for(Menu menu : menuLocalization.getMenus()){
				if(menu.getLang().equals(LocalizationRequest)){
					menuLocalization.getMenus().clear();
					menuLocalization.getMenus().add(menu);					
					return renderMenuLocalizationToEdit(menuLocalization, 0);
				}
			}
			
			Iterator<Menu> iterator = menuLocalization.getMenus().iterator();
			Menu parentMenu = iterator.next().getMenu();
			
			//find parent with the same language
			int parent = -1;
			if(parentMenu != null)
			{
				MenuLocalization parentMenuLocalization = parentMenu.getMenuLocalization();
				for(Menu menu : parentMenuLocalization.getMenus()){
					if(menu.getLang().equals(LocalizationRequest)){
						parent = menu.getMenuID();
					}
				}
			}
						
			menuLocalization.getMenus().clear();

			return renderMenuLocalizationToEdit(menuLocalization, parent);

		} catch (Exception e) {
			e.printStackTrace();
			return RenderMapUtils.getModelMapError("Error retrieving Menus from database.");
		}

	}

	/**
	 * format a menu to match a json format acceptable by EXTJS
	 */
	
	private Map<String, ? extends Object> renderMenuLocalizationToEdit(MenuLocalization menuLocalization, int parent) {
		Set<Menu> menus = menuLocalization.getMenus();
		Map<String, Object> data = new ConcurrentHashMap<String, Object>();
		data.put("menuLocalizationID", menuLocalization.getMenuLocalizationID());
		if(parent > 0){
			data.put("parentMenuID",parent);
		}
		else 
			data.put("flag",parent == 0? true : false);
		if(menus.size() > 0){
			Menu menu = menus.iterator().next();
			data.put("title", menu.getTitle());
			data.put("menuID", menu.getMenuID());
			data.put("title", menu.getTitle());
			data.put("lang", menu.getLang());
			data.put("permalink", menu.getPermalink());
			data.put("layout", menu.getLayout() == null ? "" : menu.getLayout());
			data.put("disabled",
					menu.getDisabled() == null ? 0 : menu.getDisabled());
			data.put("view", menu.getView() == null ? "" : menu.getView());
			data.put("parentMenuID", menu.getMenu() == null ? 0 : menu.getMenu().getMenuID());
		}
		return RenderMapUtils.getMap(data);
	}
	
	

}
